// pages/my/integral/index.js
const App=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      title:'',  //头部标题
      list:[],
      count:0,
      moeny:0,
      offset:1,//页码
      limit:20, //条数
      doneLoading: false,
      dataState: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.title){
      wx.setNavigationBarTitle({
        title: options.title
      })
    }
   
    
   this.setData({
      title: options.title    // 其他页面传过来的标题名
    },()=>{
      this.record();
    })
    
  },
  record(){
    let title=this.data.title;
    let moeny = wx.getStorageSync('userInfo').balance;
    let list = this.data.list;
    let that=this;
    if (title == '余额明细'){
      App.ajax(App.Api.record, 'GET', {}).then(res => {
        console.log('qwe',res)
        list=[...res.data.rows]
        that.setData({
          list,
          count: res.data.count || 0,
          moeny
        })
      }).catch(err => {
          console.log('报错了', err)
        })
      }else if(title == '能量明细'){
      let offset = this.data.offset, limit = this.data.limit;
          let energy = wx.getStorageSync('userInfo').energy;
          this.setData({
            moeny: energy
          })
         App.ajax('mp/energy/list', 'GET', { offset: offset, limit: limit}).then(res => {
              console.log('qwe', res)
           if (res.data.rows.length == 0) {
             this.setData({
               dataState: false
             })
           }
           if (res && res.data.rows) {
             res.data.rows.map((i) => {
               list.push(i)
             })
             this.setData({
               list,
               doneLoading: false
             })
           }
             
            }).catch(err => {
              console.log('报错了', err)
            })
      }
  },
  update() {
    if (this.data.title == '余额明细'){return}
    console.log(this.data.dataState, this.data.doneLoading)
    if (!this.data.dataState || this.data.doneLoading) { return }

    let offset = this.data.offset, that = this;
    offset += 1;
    this.setData({
      offset,
      doneLoading: true
    }, () => {
      this.record();
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      offset: 1,//页码
      doneLoading: false,
      dataState: true,
      list:[]
    },()=>{
      this.record();
    })
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})